/*
 * 
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mygameframework.isoengine;

import com.mygameframework.isoengine.Scroller.SCROLLER_WRAP_MODE;
import com.sun.imageio.plugins.common.I18NImpl;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

/**
 *Ｔile
 * @author bill
 */
public class TileEngine {
    private TilePlotter plotter;
    private TileWalker walker;
    private Scroller scroller;
    private MouseMapper mmp;
    /**screenSpace.x;*/
    private int offsetX;
    /**screenSpace.y;*/
    private int offsetY;
    private int tileWidth;
    private int tileHeight;
    private int mapWidth;
    private int mapHeight;
    private int halfMapWidth;
    private int halfMapHeight;
    public TileEngine(Rectangle screenSpace ,Rectangle dstRect,
            MapType mapType,int tw,int th,int mw,int mh,
            SCROLLER_WRAP_MODE h,SCROLLER_WRAP_MODE v
//            ,int adjustXoffset,int adjustYoffset
            ) {
        offsetX=screenSpace.x;
        offsetY=screenSpace.y;
        this.tileWidth=tw;
        this.tileHeight=th;
        this.mapWidth=mw;
        this.mapHeight=mh;
        halfMapWidth=this.mapWidth>>1;
        halfMapHeight=this.mapHeight>>1;
                
        plotter=new TilePlotter();
        plotter.setMapType(mapType);
        plotter.setTileSize(tw, th);
        
        walker=new TileWalker();
        walker.setMapType(mapType);
        
        scroller = new Scroller();
        scroller.setMapSize(mw, mh);
        scroller.setPlotter(plotter);
        scroller.setHorizontalMode(h);
        scroller.setVerticalMode(v);
        scroller.setScreeSpace(screenSpace);
        scroller.calculateWorldSpace(plotter, dstRect,mw, mh);
        scroller.calculateAnchorSpace();
        
        mmp=new MouseMapper(walker, scroller, tw, th);
        mmp.calcOriginPoint(plotter,dstRect.x, dstRect.y);   
    }
    
    public Point plot(Point ptMap){
        return getPlotter().plotTile(ptMap);
    }
    public void reCalcOffsetAndAnchorSpace(Rectangle screenSpace){
        offsetX=screenSpace.x;
        offsetY=screenSpace.y;
        getScroller().setScreeSpace(screenSpace);
//        getScroller().calculateWorldSpace();//不用计算，是不变值
        getScroller().calculateAnchorSpace();
    }
    public Point mapToWorld(Point ptMap){
        return this.plotter.plotTile(ptMap);
    }
    public Point worldToScreen(Point ptWorld){
        return this.scroller.worldToScreen(ptWorld);
    }
    /**
     * screen to map
     * 如果鼠标位置相对于Frame,就该位置是含框边的宽dx,标题栏的高为dy<br>
     * 即在视窗左上角的点非（0，0），而是（dx,dy）
     * 用户不能把鼠标位置预减dx,dy,这是没必要的(方法内部会帮你减的)<br>
     * <br>
     * 如果是JPanel或Canvas,鼠标位置是不含偏移<br>
     * @param ptScreenMouse 未减 框边\title bar(若是存在)偏移值的屏幕鼠标位置,
     * @return 
     */
    public Point mouseMap(Point ptScreenMouse){
        return getMouseMapper().mapMouse(ptScreenMouse);
    }
    public void moveScreenAnchor(int dx,int dy,boolean needCheck){
        getScroller().moveScreenAnchor(dx, dy, needCheck);
    }
   
    public TilePlotter getPlotter() {
        return plotter;
    }

    public Scroller getScroller() {
        return scroller;
    }

    public TileWalker getWalker() {
        return walker;
    }

    public MouseMapper getMouseMapper() {
        return mmp;
    }
    public Rectangle getScreenSpace(){
        return getScroller().getScreeSpace();
    }
    public Rectangle getAnchorSpace(){
        return getScroller().getAnchorSpace();
    }
    public Rectangle getWorldSpace(){
        return getScroller().getWorldSpace();
    }
    public void setScreenAnchor(int x,int y){
        getScreenAnchor().x=x;
        getScreenAnchor().y=y;
    }
    public int getScreenAnchorX(){
        return getScroller().getScreenAnchor().x;
    }
    public int getScreenAnchorY(){
        return getScroller().getScreenAnchor().y;
    }
    public Point getScreenAnchor(){
        return getScroller().getScreenAnchor();
    }
    /**screenSpace.x;*/
    public int getOffsetX() {
        return offsetX;
    }

    /**screenSpace.y;*/
    public int getOffsetY() {
        return offsetY;
    }

    public int getMapHeight() {
        return mapHeight;
    }

    public int getMapWidth() {
        return mapWidth;
    }

    public int getHalfMapHeight() {
        return halfMapHeight;
    }

    public void setHalfMapHeight(int halfMapHeight) {
        this.halfMapHeight = halfMapHeight;
    }

    public int getHalfMapWidth() {
        return halfMapWidth;
    }

    public void setHalfMapWidth(int halfMapWidth) {
        this.halfMapWidth = halfMapWidth;
    }

    public int getTileHeight() {
        return tileHeight;
    }

    public int getTileWidth() {
        return tileWidth;
    }
    
}
